#include "data_global.h"
#include "data_global.c"
#include "sqlite_link_list.h"
extern unsigned char dev_buzzer_mask;
extern pthread_cond_t cond_uart_cmd;
extern pthread_cond_t cond_refresh_updata;
extern unsigned char buzzer_mask;
extern pthread_mutex_t mutex_slinklist;
void *pthread_analysis(void *args)
{
	char flags = 0;
	while(1)
	{
		pthread_mutex_lock(&mutex_analysis);
		puts("before cond_analysis");
		pthread_cond_wait(&cond_analysis,&mutex_analysis);
		puts("after cond_analysis");
#if 1
		pthread_mutex_lock(&mutex_slinklist);
			if(!sqlite_EmptyLinklist(slinkHead))//链表部位空
			{
				flags = 0;
				puts("linklist Normal");
				if(slinkHead->next->storageNum == 1)
				{
					puts("storage 1");
					if(slinkHead->next->data_link.storage_no[1].temperature > slinkHead->next->data_link.storage_no[1].temperatureMAX)
					{
						puts("storage 1 temperature too high");
						SMS_MSG = SMS_MSG_TEM1;
						pthread_mutex_lock(&mutex_buzzer);
						dev_uart_mask = 0x51;
						pthread_cond_signal(&cond_uart_cmd);
						dev_uart_mask = 0x43;
						pthread_cond_signal(&cond_uart_cmd);
						pthread_cond_signal(&cond_sms);
						pthread_mutex_unlock(&mutex_buzzer);
						flags = 1;
					}

					else if(slinkHead->next->data_link.storage_no[1].temperature < slinkHead->next->data_link.storage_no[1].temperatureMIN)
					{
						puts("storage 1 temperature too low");
						SMS_MSG = SMS_MSG_TEM1;
						pthread_mutex_lock(&mutex_buzzer);
						pthread_cond_signal(&cond_sms);
						flags = 1;
						pthread_mutex_unlock(&mutex_buzzer);
					}

					else if(slinkHead->next->data_link.storage_no[1].humidity > slinkHead->next->data_link.storage_no[1].humidityMAX)
					{
						puts("storage 1 humidity too high");
						SMS_MSG = SMS_MSG_HUM1;
						pthread_mutex_lock(&mutex_buzzer);
						dev_uart_mask = 0x51;
						pthread_cond_signal(&cond_uart_cmd);
						flags = 1;
						pthread_cond_signal(&cond_sms);
						//        pthread_cond_signal(&cond_sms);
						//       pthread_cond_signal(&cond_buzzer);
						pthread_mutex_unlock(&mutex_buzzer);
					}

					else if(slinkHead->next->data_link.storage_no[1].humidity < slinkHead->next->data_link.storage_no[1].humidityMIN)
					{
						puts("storage 1 humidity too low");
						SMS_MSG = SMS_MSG_HUM1;
						pthread_mutex_lock(&mutex_buzzer);
						flags = 1;
						pthread_cond_signal(&cond_sms);
						//         pthread_cond_signal(&cond_sms);
						//         pthread_cond_signal(&cond_buzzer);
						pthread_mutex_unlock(&mutex_buzzer);
					}

					else if(slinkHead->next->data_link.storage_no[1].illumination > slinkHead->next->data_link.storage_no[1].illuminationMAX)
					{
						puts("storage 1 illumination too high");
						dev_uart_mask = 0x51;
						pthread_cond_signal(&cond_uart_cmd);
						SMS_MSG = SMS_MSG_ILL1;
						flags = 1;
						pthread_mutex_lock(&mutex_buzzer);
						pthread_cond_signal(&cond_sms);
						//  pthread_cond_signal(&cond_sms);
						//  pthread_cond_signal(&cond_buzzer);
						pthread_mutex_unlock(&mutex_buzzer);
					}
					else if(slinkHead->next->data_link.storage_no[1].illumination < slinkHead->next->data_link.storage_no[1].illuminationMIN)
					{
						puts("++++++++++++++++++++++++++++++++++++++++++++++");
						puts("storage 1 illumination too low");
						dev_uart_mask = 0x51;
						flags = 1;
						pthread_cond_signal(&cond_uart_cmd);
						SMS_MSG = SMS_MSG_ILL1;
						pthread_mutex_lock(&mutex_buzzer);
						pthread_cond_signal(&cond_sms);
						// pthread_cond_signal(&cond_sms);
						//  pthread_cond_signal(&cond_buzzer);
						pthread_mutex_unlock(&mutex_buzzer);
					}
					else if(buzzer_mask == 0x51)
					{
						dev_uart_mask = 0x51;
						pthread_cond_signal(&cond_uart_cmd);
						
					}
					else
					{
						dev_uart_mask = 0x50;
						pthread_cond_signal(&cond_uart_cmd);

					}

					if(slinkHead->next->storageNum == 2)
					{
						puts("storage 2");
						if(slinkHead->next->data_link.storage_no[2].temperature > slinkHead->next->data_link.storage_no[2].temperatureMAX)
						{
							puts("temperature too high");
							SMS_MSG = SMS_MSG_TEM2;
							pthread_mutex_lock(&mutex_buzzer);
							dev_uart_mask = 0x51;
							flags = 1;
							pthread_cond_signal(&cond_uart_cmd);
							pthread_cond_signal(&cond_sms);
							pthread_mutex_unlock(&mutex_buzzer);
						}

						if(slinkHead->next->data_link.storage_no[2].temperature < slinkHead->next->data_link.storage_no[2].temperatureMIN)
						{
							puts("temperature too low");
							SMS_MSG = SMS_MSG_TEM2;
							pthread_mutex_lock(&mutex_buzzer);
							dev_uart_mask = 0x51;
							pthread_cond_signal(&cond_uart_cmd);
							flags = 1;
							pthread_cond_signal(&cond_sms);
							pthread_mutex_unlock(&mutex_buzzer);
						}

						if(slinkHead->next->data_link.storage_no[2].humidity > slinkHead->next->data_link.storage_no[2].humidityMAX)
						{
							puts("humidity too high");
							SMS_MSG = SMS_MSG_HUM2;
							pthread_mutex_lock(&mutex_buzzer);
							dev_uart_mask = 0x51;
							flags = 1;
							pthread_cond_signal(&cond_uart_cmd);
							pthread_cond_signal(&cond_sms);
							pthread_mutex_unlock(&mutex_buzzer);
						}

						if(slinkHead->next->data_link.storage_no[2].humidity < slinkHead->next->data_link.storage_no[2].humidityMIN)
						{
							puts("humidity too low");
							SMS_MSG = SMS_MSG_HUM2;
							pthread_mutex_lock(&mutex_buzzer);
							dev_uart_mask = 0x51;
							flags = 1;
							pthread_cond_signal(&cond_uart_cmd);
							pthread_cond_signal(&cond_sms);
							pthread_mutex_unlock(&mutex_buzzer);
						}

						if(slinkHead->next->data_link.storage_no[2].illumination > slinkHead->next->data_link.storage_no[2].illuminationMAX)
						{
							puts("illumination too high");
							dev_buzzer_mask = 1;
							SMS_MSG = SMS_MSG_ILL2;
							pthread_mutex_lock(&mutex_buzzer);
							dev_uart_mask = 0x51;
							flags = 1;
							pthread_cond_signal(&cond_uart_cmd);
							pthread_cond_signal(&cond_sms);
							pthread_mutex_unlock(&mutex_buzzer);
						}
						if(slinkHead->next->data_link.storage_no[2].illumination < slinkHead->next->data_link.storage_no[2].illuminationMIN)
						{
							puts("illumination too low");
							SMS_MSG = SMS_MSG_ILL2;
							pthread_mutex_lock(&mutex_buzzer);
							dev_uart_mask = 0x51;
							flags = 1;
							pthread_cond_signal(&cond_uart_cmd);
							pthread_cond_signal(&cond_sms);
							pthread_mutex_unlock(&mutex_buzzer);
						}
					}
				}
				else
				{
					puts("linklist is empty");
				}
//				pthread_cond_signal(&cond_refresh_updata);
				sqlite_dellinknode(slinkHead);
				pthread_mutex_unlock(&mutex_slinklist);
				pthread_mutex_unlock(&mutex_analysis);
			}
#endif
			//	pthread_cond_signal(&cond_refresh_updata);
	}
	return args;
}
